package com.linln.admin.integral.mall.controller;

import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.modules.integralmall.domain.IntegralMallService;
import com.linln.modules.integralmall.service.IntegralMallServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.integral.mall
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.integral.mall
 * @Description: TODO
 * @date Date : 2020年12月14日 15:04
 */
@Controller
@RequestMapping("integralMall")
@Slf4j
public class IntegralMallServiceController {
    @RequestMapping("/index")
    public String index() {
        return "/integralMall/index";
    }

    @Autowired
    private IntegralMallServiceService integralMallService;

    @GetMapping
    @ResponseBody
    public Map<String, Object> getIntegralMallService(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
                                                    ,String serviceName) {
        Map<String, Object> service = null;
        try {
            service = integralMallService.getIntegralMallService(page, limit,serviceName);
            service.put("code", 0);
        } catch (Exception e) {
            service = new HashMap<>();
            service.put("code", 500);
            service.put("msg", "获取服务列表失败");
            log.error(e.getMessage());
        }
        return service;
    }
    @GetMapping("getNameById/{id}")
    @ResponseBody
    public JsonResult getIntegralMallServiceNameById(
            @PathVariable("id") String imsId) {
        IntegralMallService integralMallServiceById = integralMallService.getIntegralMallServiceById(imsId);
        return JsonResult.ok().put("imgName",integralMallServiceById.getServiceName());
    }

    @PostMapping
    @ResponseBody
    public JsonResult addIntegralMallService(@RequestParam(value = "files", required = false) List<MultipartFile> files, IntegralMallService service) {
        try {
            service.setImsContent(SessionUtil.getRequest().getParameter("imsContent"));
            integralMallService.addIntegralMallService(files, service);
            return JsonResult.ok("保存成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("保存失败");
        }
    }

    @PutMapping
    @ResponseBody
    public JsonResult updateIntegralMallService(@RequestParam(value = "files", required = false) List<MultipartFile> files, @RequestParam("status") List<Integer> status, IntegralMallService service) {
        try {
            integralMallService.updateIntegralMallService(files, status, service);
            return JsonResult.ok("更新成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("更新失败");
        }
    }


    @DeleteMapping("/{serviceId}")
    @ResponseBody
    public JsonResult delIntegralMallService(@PathVariable("serviceId") String serviceId) {
        try {
            if (integralMallService.delIntegralMallService(serviceId)) {
                return JsonResult.ok("删除成功");
            }
            return JsonResult.error("删除失败");
        } catch (Exception e) {
            log.error(e.getMessage());
            return JsonResult.error("删除失败");
        }
    }
}
